//
//  CityInfoChooseView.m
//  CityInfoChooseView
//
//  Created by 正合适 on 2017/10/12.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "CityInfoChooseView.h"
#import "CityInfoAreaListInfoView.h"
#import "CityInfoModel.h"
#import "CityInfoSelectedBtnsView.h"
#import "CityInfoChooseMacro.h"

@interface CityInfoChooseView ()
/**选择界面view*/
@property (nonatomic, strong) UIView *bgView;
/**点击手势view*/
@property (nonatomic, strong) UIView *taoGestureView;
/**列表view*/
@property (nonatomic, strong) CityInfoAreaListInfoView *listView;
/**选择view*/
@property (nonatomic, strong) CityInfoSelectedBtnsView *selectView;
/**数据源*/
@property (nonatomic, strong) NSMutableArray *datasource;
/**<#desc#>*/
@property (nonatomic, assign) NSInteger currentIndex;
@end

@implementation CityInfoChooseView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    if (self) {
        [self defaultData];
        [self createUI];
        [self addGestures];
    }
    return self;
}

- (instancetype)init {
    return [self initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
}

#pragma mark - 初始化数据
- (void)defaultData {
    _currentIndex = 0;
    _datasource = [NSMutableArray array];
    // 查看数据库表结构
    NSArray *arr = [CityInfoModel loadCityInfoFromDatabaseId:@"100000"];
    [_datasource addObject:arr.mutableCopy];
}

#pragma mark - 初始化界面
- (void)createUI {
    // 背景
    self.backgroundColor = RGBAColor(0, 0, 0, 0.4);
    
    // 选择界面
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, ScreenWidth, 4.0*ScreenHeight/7.0)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    // 标题
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18];
    label.text = @"所在地区";
    label.textColor = RGBAColor(102, 102, 102, 1);
    [_bgView addSubview:label];
    
    // 关闭按钮
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(_bgView.frame.size.width-30, 17, 15, 15)];
    [closeBtn setImage:[UIImage imageNamed:@"ZHUIKit.bundle/choose_guanbi"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:closeBtn];
    
    // 背景点击关闭
    _taoGestureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-_bgView.frame.size.height)];
    [self addSubview:_taoGestureView];
    
    __weak typeof(self) weakSelf = self;
    // 选择view
    _selectView = [[CityInfoSelectedBtnsView alloc] initWithFrame:CGRectMake(0, label.frame.origin.y+label.frame.size.height, _bgView.frame.size.width, 50)];
    [_bgView addSubview:_selectView];
    
    // listview
    _listView = [[CityInfoAreaListInfoView alloc] initWithFrame:CGRectMake(0, _selectView.frame.origin.y+_selectView.frame.size.height, _bgView.frame.size.width, _bgView.frame.size.height-label.frame.size.height-_selectView.frame.size.height)];
    [_bgView addSubview:_listView];
    
    _listView.listViewEndScrollBlock = ^(NSInteger index, NSArray<CityInfoModel *> *datas, NSInteger isClose) {
        NSMutableArray *arr = [NSMutableArray array];
        for (CityInfoModel *model in datas) {
            [arr addObject:model.areaName];
        }
        [weakSelf.selectView configTitles:arr index:index];
        // 最大个数为3个
        if (index == 2 && arr.count == 3 && isClose) {
            [weakSelf dealResultWith:datas];
            [weakSelf closeAction];
        }
    };
    _selectView.cityInfoSelectedBtnClickBlock = ^(NSInteger index) {
        [weakSelf.listView configScrollToPageWithIndex:index];
    };
}

#pragma mark - 处理选择结果
- (void)dealResultWith:(NSArray<CityInfoModel *>*)result {
    NSMutableArray *resultArr = [NSMutableArray array];
    for (CityInfoModel *model in result) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:model.areaId forKey:@"areaId"];
        [dic setObject:model.areaName forKey:@"areaName"];
        [resultArr addObject:dic];
    }
    if (self.resultBlock) {
        self.resultBlock(resultArr);
    }
}

#pragma mark - 添加手势
- (void)addGestures {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)];
    [_taoGestureView addGestureRecognizer:tap];
}

#pragma mark - 关闭按钮action
- (void)closeAction {
    [self animationWithOpen:NO];
}

#pragma mark - 开启
- (void)open {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self animationWithOpen:YES];
}

#pragma mark - 开启
- (void)openBeforeSomething:(void(^)(void))doSomething {
    if (doSomething) {
        doSomething();
    }
    [self open];
}

#pragma mark - 开启
- (void)showBeforeSomething:(void(^)(void))doSomething areas:(NSArray <NSDictionary *>*)areas {
    [_listView configAreas:areas];
    [self openBeforeSomething:doSomething];
}

#pragma mark - 根据区域id显示指定的数据
- (void)configWithAreas:(NSArray <NSDictionary *>*)areas {
    [_listView configAreas:areas];
}

#pragma mark - 开启关闭动画
- (void)animationWithOpen:(BOOL)isOpen {
    __weak typeof(self) weakself = self;
    [UIView animateWithDuration:0.5 animations:^{
        if (isOpen) {
            weakself.bgView.frame = CGRectMake(0, weakself.frame.size.height-weakself.bgView.frame.size.height, weakself.bgView.frame.size.width, weakself.bgView.frame.size.height);
        } else {
             weakself.bgView.frame = CGRectMake(0, weakself.frame.size.height, weakself.bgView.frame.size.width, weakself.bgView.frame.size.height);
        }
    } completion:^(BOOL finished) {
        if (!isOpen) {
            [weakself removeFromSuperview];
        }
    }];
}



@end
